<template>
  <div>
    <h3 style="text-align: center;">促销方案审核</h3>
    <div style="height:250px">
      <el-table
          :data="cutObj.records"
          style="width: 100%"
          border stripe>
        <el-table-column prop="number" label="促销方案编号"/>
        <el-table-column prop="description" label="促销理由"/>
        <el-table-column prop="status" label="订单状态"/>
        <el-table-column prop="audit" label="审核状态"/>
        <el-table-column prop="rate" label="促销率"/>
        <el-table-column fixed="right" label="审核">
          <template #default="scope">
            <el-button type="success" size="small" :disabled="scope.row.audit!='待审核'" @click="updateYes(scope.row.promotionalId)">通过</el-button>
            <el-button type="warning" size="small" :disabled="scope.row.audit!='待审核'" @click="updateNo(scope.row.promotionalId)">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="margin: 20px">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="cutObj.size"
          background
          layout="prev, pager, next, jumper"
          :total="cutObj.total"
          @current-change="findByPage"
      />
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      cutObj: [],
      currentPage: 1,
    }
  }, methods: {
    findByPage(pageNum) {
      axios.get("/project/promotional/findByPage", {
        params: {pageNum}
      }).then(resp => {
        resp.data.records.forEach(n=>{
          if (n.status == 0){
            n.status = "未结束"
          }else {
            n.status = "结束"
          }
          if (n.audit == 0){
            n.audit = "待审核"
          }else if (n.audit == 1){
            n.audit = "审核通过"
          }else {
            n.audit = "审核不通过"
          }
        })
        console.log(resp.data)
        this.cutObj = resp.data
      })
    },
    updateYes(promotionalId){
      axios.get("/project/promotional/update",{
        params:{
          promotionalId:promotionalId,
          audit:1
        }
      }).then(resp=>{
        if (resp.data.tag == 0){
          this.$message({
            message: "审核成功",
            type: "success",
            showClose: true,
          });
        }else {
          this.$message({
            message: "审核失败",
            type: "error",
            showClose: true,
          });
        }
        this.findByPage(1)
      })
    },
    updateNo(promotionalId){
      axios.get("/project/promotional/update",{
        params:{
          promotionalId:promotionalId,
          audit:2
        }
      }).then(resp=>{
        if (resp.data.tag == 0){
          this.$message({
            message: "审核成功",
            type: "success",
            showClose: true,
          });
        }else {
          this.$message({
            message: "审核失败",
            type: "error",
            showClose: true,
          });
        }
        this.findByPage(1)
      })
    }
  }, created() {
    this.findByPage(1);
  }
}
</script>

<style scoped>

</style>